/**
 **************************************************************************
 * @file     at32f421_int.c
 * @brief    main interrupt service routines.
 **************************************************************************
 *                       Copyright notice & Disclaimer
 *
 * The software Board Support Package (BSP) that is made available to
 * download from Artery official website is the copyrighted work of Artery.
 * Artery authorizes customers to use, copy, and distribute the BSP
 * software and its related documentation for the purpose of design and
 * development in conjunction with Artery microcontrollers. Use of the
 * software is governed by this copyright notice and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 **************************************************************************
 */

/* includes ------------------------------------------------------------------*/
#include "at32f421_int.h"
#include <string.h>
#define COUNTOF(a)                       (sizeof(a) / sizeof(*(a)))
#define USART2_TX_BUFFER_SIZE            (COUNTOF(usart2_tx_buffer) - 1)
#define USART1_TX_BUFFER_SIZE            (COUNTOF(usart1_tx_buffer) - 1)
extern void bled(confirm_state new_state);
extern void gled(confirm_state new_state);
extern uint8_t usart2_tx_dma_status;
extern uint8_t usart2_rx_dma_status;
// extern uint8_t usart1_tx_dma_status;
// extern uint8_t usart1_rx_dma_status;
volatile uint32_t msTick = 0;

uint32_t millis(void)
{
	return msTick;
}
void delay_ms(uint32_t ms)
{
	uint32_t start = millis();
	while (millis() < (start + ms))
		;
}
/** @addtogroup AT32F421_periph_examples
 * @{
 */

/** @addtogroup 421_USART_transfer_by_dma_interrupt
 * @{
 */

/**
 * @brief  this function handles nmi exception.
 * @param  none
 * @retval none
 */
void NMI_Handler(void)
{
}

/**
 * @brief  this function handles hard fault exception.
 * @param  none
 * @retval none
 */
void HardFault_Handler(void)
{
	/* go to infinite loop when hard fault exception occurs */
	while (1)
	{
	}
}

/**
 * @brief  this function handles memory manage exception.
 * @param  none
 * @retval none
 */
void MemManage_Handler(void)
{
	/* go to infinite loop when memory manage exception occurs */
	while (1)
	{
	}
}

/**
 * @brief  this function handles bus fault exception.
 * @param  none
 * @retval none
 */
void BusFault_Handler(void)
{
	/* go to infinite loop when bus fault exception occurs */
	while (1)
	{
	}
}

/**
 * @brief  this function handles usage fault exception.
 * @param  none
 * @retval none
 */
void UsageFault_Handler(void)
{
	/* go to infinite loop when usage fault exception occurs */
	while (1)
	{
	}
}

/**
 * @brief  this function handles svcall exception.
 * @param  none
 * @retval none
 */
void SVC_Handler(void)
{
}

/**
 * @brief  this function handles debug monitor exception.
 * @param  none
 * @retval none
 */
void DebugMon_Handler(void)
{
}

/**
 * @brief  this function handles pendsv_handler exception.
 * @param  none
 * @retval none
 */
void PendSV_Handler(void)
{
}

/**
 * @brief  this function handles systick handler.
 * @param  none
 * @retval none
 */
void SysTick_Handler(void)
{
	msTick++;
}

/**
 * @brief  this function handles dma1 channel2/3 handler.
 * @param  none
 * @retval none
 */
// void DMA1_Channel3_2_IRQHandler(void)
// {
// 	if (dma_interrupt_flag_get(DMA1_FDT2_FLAG))
// 	{
// 		usart1_tx_dma_status = 1;
// 		dma_flag_clear(DMA1_FDT2_FLAG);
// 		dma_channel_enable(DMA1_CHANNEL2, FALSE);

// 	}
// 	else if (dma_interrupt_flag_get(DMA1_FDT3_FLAG))
// 	{
// 		usart1_rx_dma_status = 1;
// 		dma_flag_clear(DMA1_FDT3_FLAG);
// 		dma_channel_enable(DMA1_CHANNEL3, FALSE);
// 	}
// }

/**
 * @brief  this function handles dma1 channel4/5 handler.
 * @param  none
 * @retval none
 */
void DMA1_Channel5_4_IRQHandler(void)
{
	if (dma_interrupt_flag_get(DMA1_FDT4_FLAG))
	{
		usart2_tx_dma_status = 0;
		dma_flag_clear(DMA1_FDT4_FLAG);
		dma_channel_enable(DMA1_CHANNEL4, FALSE);
		gled(0);
		// memset(usart2_tx_buffer, 0, sizeof(usart2_tx_buffer));
	}
	if (dma_interrupt_flag_get(DMA1_FDT5_FLAG))
	{
		usart2_rx_dma_status = 1;
		dma_flag_clear(DMA1_FDT5_FLAG);
		// dma_channel_enable(DMA1_CHANNEL5, FALSE);
		// bled(1);
		// memset(usart2_rx_buffer, 0, 10);
	}
}

/**
 * @}
 */

/**
 * @}
 */
